﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Windows;
using System.Data.SqlClient;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdReqTrace.xaml
    /// </summary>
    public partial class wdReqTrace : Window
    {
        public wdReqTrace()
        {
            InitializeComponent();
            loadProject();
            
        }
        public void loadProject()
        {
            String strSQL = "select proName from project";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbPro.Items.Add(dt.Rows[i][0]);                
            }
            cbPro.SelectedIndex = 0;            
            ketnoi.closeConnection();  
        }
        public void loadReq()
        {
            clsProject pro = new clsProject();
            int idProject = Convert.ToInt32(pro.getIdFromName(cbPro.Text));
            String strSQL = "select idReq from req where idPro='"+idProject+"'";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbReq.Items.Add(dt.Rows[i][0]);
            }
            cbReq.SelectedIndex = 0;
            ketnoi.closeConnection();
        }
        public void loadReqScreen()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            String strSQL1 = "select Top 1 ID from project ";
            SqlDataAdapter da1 = new SqlDataAdapter(strSQL1, ketnoi.Connection);
            DataTable dt1 = new DataTable();
            da1.Fill(dt1);
            clsProject pro = new clsProject();
            int idProject = Convert.ToInt32(dt1.Rows[0][0].ToString());
            String strSQL = "select idReq from req where idPro='" + idProject + "'";            
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbReq.Items.Add(dt.Rows[i][0]);
            }
            cbReq.SelectedIndex = 0;
            ketnoi.closeConnection();
        }
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            clsProject pro = new clsProject();
            reqTrace reqTrace = new reqTrace();
            reqTrace.IdPro = Convert.ToInt32(pro.getIdFromName(cbPro.Text));
            reqTrace.IdReq = cbReq.Text;
            reqTrace.StartDay = dpStart.SelectedDate;
            reqTrace.Effort = Convert.ToInt32(tbEffort.Text);
            reqTrace.CompleteEffort = Convert.ToInt32(tbCom.Text);
            reqTrace.Assignment = tbAss.Text;
            reqTrace.Cost = Convert.ToInt32(tbCost.Text);
            if (reqTrace.StartDay == null || tbEffort.Text == "" || tbCom.Text == "" || tbAss.Text == "" || tbCost.Text == "")
            {
                System.Windows.MessageBox.Show("Values must be inserted");
            }
            else if (reqTrace.chectProAndReq(cbPro.Text, cbReq.Text) == 0)
            {
                reqTrace.insertReqTrace();
                System.Windows.MessageBox.Show("Insert Success","Message",MessageBoxButton.OK,MessageBoxImage.Information);
            }
            else if (reqTrace.chectProAndReq(cbPro.Text, cbReq.Text) > 0)
            {
                reqTrace.updateReqTrace();
                System.Windows.MessageBox.Show("Update Success", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
        }             
                   
       

        private void btnFreshReq_Click(object sender, RoutedEventArgs e)
        {
            cbReq.Items.Clear();
            loadReq();
        }

        private void btnShow_Click(object sender, RoutedEventArgs e)
        {
            wdGantt gantt = new wdGantt();
            gantt.Message = cbPro.Text;
            gantt.Show();
        }

       
    }
}
